package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Quality;
import com.ruoyi.system.service.IQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 质量管理Controller
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/system/quality")
public class QualityController extends BaseController
{
    @Autowired
    private IQualityService qualityService;

    /**
     * 查询质量管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:quality:list')")
    @GetMapping("/list")
    public TableDataInfo list(Quality quality)
    {
        startPage();
        List<Quality> list = qualityService.selectQualityList(quality);
        return getDataTable(list);
    }

    /**
     * 导出质量管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:quality:export')")
    @Log(title = "质量管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Quality quality)
    {
        List<Quality> list = qualityService.selectQualityList(quality);
        ExcelUtil<Quality> util = new ExcelUtil<Quality>(Quality.class);
        util.exportExcel(response, list, "质量管理数据");
    }

    @Log(title = "质量管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:quality:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Quality> util = new ExcelUtil<Quality>(Quality.class);
        List<Quality> qualitiesList = util.importExcel(file.getInputStream());
        String message = qualityService.importQuality(qualitiesList, updateSupport);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Quality> util = new ExcelUtil<Quality>(Quality.class);
        util.importTemplateExcel(response, "质量数据");
    }
    /**
     * 获取质量管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:quality:query')")
    @GetMapping(value = "/{qualityId}")
    public AjaxResult getInfo(@PathVariable("qualityId") Long qualityId)
    {
        return success(qualityService.selectQualityByQualityId(qualityId));
    }

    /**
     * 新增质量管理
     */
    @PreAuthorize("@ss.hasPermi('system:quality:add')")
    @Log(title = "质量管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Quality quality)
    {
        return toAjax(qualityService.insertQuality(quality));
    }

    /**
     * 修改质量管理
     */
    @PreAuthorize("@ss.hasPermi('system:quality:edit')")
    @Log(title = "质量管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Quality quality)
    {
        return toAjax(qualityService.updateQuality(quality));
    }

    /**
     * 删除质量管理
     */
    @PreAuthorize("@ss.hasPermi('system:quality:remove')")
    @Log(title = "质量管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qualityIds}")
    public AjaxResult remove(@PathVariable Long[] qualityIds)
    {
        return toAjax(qualityService.deleteQualityByQualityIds(qualityIds));
    }
}
